<include file="public@header" />
<style type="text/css">
	.well{
		text-align: right;
		padding-right: 10px;
	}
	.sql_text{
		height: 95%;
	}
</style>
</head>
<body>
	<div class="wrap js-check-wrap">
		<ul class="nav nav-tabs">
			<li class="active"><a href="{:url('GPSCategory/index')}">分类管理</a></li>
		</ul>
        <form class="well form-inline margin-top-20" method="post" action="{:url('User/index')}">
            <a class="btn btn-success add_category" href="#">添加分类</a>
        </form>
		<table class="table table-hover table-bordered">
			<thead>
				<tr>
					<th width="50">ID</th>
					<th>名称</th>
					<th>状态</th>
					<th>创建时间</th>
					<th>更新时间</th>
					<th width="130">{:lang('ACTIONS')}</th>
				</tr>
			</thead>
			<tbody>
				<foreach name="category" item="vo">
				<tr>
					<td>{$vo.id}</td>
					<td><a href="{:url("GPSCategory/showItem")}?id={$vo.id}" class="show_item">{$vo.category_name} - [ {$vo.hex_name} ]</a></td>
					<td>
						<switch name="vo.status">
						    <case value="1">
						    	<span class="text-warning">待生成</span>
						    	<a class="btn create_table" data-category_id="{$vo.id}"><i class="fa fa-database"></i></a>
						    </case>
						    <case value="2">
					    		<span class="text-success">正常</span>
					    		<a class="btn show_sql" data-sql_title="{$vo.category_name} - [ {$vo.hex_name} ]" data-category_id="{$vo.id}"><i class="fa fa-file-text"></i></a>
					    	</case>
						    <case value="3"><span class="text-danger">删除</span></case>
						    <default /><span class="text-warning">待生成</span>
						</switch>
					</td>
					<td>{$vo.create_time|date="Y-m-d H:i:s",###}</td>
					<td>{$vo.update_time|date="Y-m-d H:i:s",###}</td>
					<td>
						<a href="javascript:void(0);" class="edit_category" data-id="{$vo.id}" data-category_name='{$vo.category_name}'>{:lang('EDIT')}</a>
						<eq name="vo.status" value="2">
						<a href="{:url('GPSDetail/index', ['category_id' => $vo.id])}" class="open_detail" data-category_name='{$vo.category_name}'>采集列表</a>
						</eq>
					</td>
				</tr>
				</foreach>
			</tbody>
		</table>
		<div class="pagination">{$page}</div>
	</div>
	<script src="__STATIC__/js/admin.js"></script>
	<script type="text/javascript">
		$(function(){
			$(".add_category").bind("click", function(){
				layer.prompt({title: '请输入分类名称', formType: 0}, function(category_name, index){
			  		$.ajax({
			  			url: '{:url('GPSCategory/add')}',
			  			type: "POST",
			  			dataType: 'json',
			  			data: {'category_name': category_name},
			  			success: function(res){
			  				if(res.code > 0){
			  					window.location.href = res.url;
			  				}else{
			  					layer.alert(res.msg, {
								  icon: 2,
								});
			  				}

		  					layer.close(index);
			  			}
			  		})
				});
			})

			$(document).on("click", ".edit_category", function(){
				var category_id = $(this).data('id');
				var category_name = $(this).data('category_name');

				layer.prompt({shadeClose: true,title: '请输入分类名称', value: category_name, formType: 0}, function(category_name, index){
			  		$.ajax({
			  			url: '{:url('GPSCategory/edit')}',
			  			type: "POST",
			  			dataType: 'json',
			  			data: {'category_id': category_id, 'category_name': category_name},
			  			success: function(res){
			  				if(res.code > 0){
			  					window.location.href = res.url;
			  				}else{
			  					layer.alert(res.msg, {
								  icon: 2,
								});
			  				}

		  					layer.close(index);
			  			}
			  		})
				});
			})

			$(document).on('click', '.show_item', function(){
				var title = "采集字段";
				var href = $(this).attr('href');

				layer.open({
				    type: 2,
				    title: title,
				    shadeClose: true,
				    shade: 0.8,
				    area: ['40%', '90%'],
				    content:  href
				}); 
				return false;
			})

			$(document).on("click", '.create_table', function(){
				var category_id = $(this).data('category_id');

				$.ajax({
		  			url: '{:url('GPSCategory/create_table')}',
		  			type: "GET",
		  			dataType: 'json',
		  			data: {'category_id': category_id},
		  			success: function(res){
		  				if(res.code > 0){
		  					window.location.href = window.location.href;
		  				}else{
		  					layer.alert(res.msg, {
							  icon: 2,
							});
		  				}
		  			}
		  		})
			})

			$(document).on("click", '.show_sql', function(){
				var category_id = $(this).data('category_id');
				var sql_title = $(this).data('sql_title');

				$.ajax({
		  			url: '{:url('GPSCategory/show_create_table')}',
		  			type: "GET",
		  			dataType: 'json',
		  			data: {'category_id': category_id},
		  			success: function(res){
		  				if(res.code > 0){
		  					layer.open({
							  type: 1,
							  title: sql_title + " 建表语句",
							  skin: 'layui-layer-demo', //样式类名
							  closeBtn: 0, //不显示关闭按钮
							  anim: 2,
							  area: ['700px', '90%'],
							  shadeClose: true, //开启遮罩关闭
							  content: '<pre class="sql_text">' + res.data.sql + '</pre>'
							});
		  				}else{
		  					layer.alert(res.msg, {
							  icon: 2,
							});
		  				}
		  			}
		  		})
			})

			$(document).on("click", '.open_detail', function(){
				parent.openapp($(this).attr('href'),$(this).attr('href'),$(this).data('category_name'),true);
				return false;
			})
		})
	</script>
</body>
</html>